import * as types from './types'

export const state = {
  form: {
    projectName: '',
    projectType: '1',
    projectAmt: '',
    repaymentType: '0',
    rateType: '1',
    rateVal: '',
    rateMinVal: '',
    rateMaxVal: '',
    cycVal: '',
    projectUse: '',
    finaAccType: '1',
    finaCertNo: '',
    finaName: '',
    mobile: '',
    isExcess: '0'
  }
}

export const getters = {
  form: state => state.form
}

export const actions = {
  submit({
    commit
  }, payload) {
    commit(types.SUBMIT, payload)
  }
}

export const mutations = {
  /**
   * 发布新标的
   * 
   * @param {any} state 
   * @param {any} {
   *     vm,
   *     formName
   *   } 
   */
  [types.SUBMIT](state, {
    vm,
    formName
  }) {
    vm.$ajax.post('/subject/create', vm.$postParams(state.form))
      .then(res => {
        if (res.data.status == vm.$appConfig.success) { // 成功
          vm.$alert('标的发布成功', '标的发布结果', {
            callback: () => {
              vm.$refs[formName].resetFields();
            }
          })
        } else {
          vm.$alert(res.data.msg, '标的发布结果', {});
        };
      })
      .catch(err => {
        vm.$notify.error({
          title: '错误',
          message: '发生了一个错误'
        });
        console.error(err);
      })
  }
}